﻿using System;
using System.Linq;
using System.Web.Mvc;
using BL.Abstract;
using BL.Class;
using BL.Generic;
using DAL.Abstract;
using DAL.Repositories;
using InventizerWEB.GenericObjects;
using Model;
using PagedList;

namespace InventizerWEB.Controllers
{
    public class UserController : Controller
    {
        private readonly IUsersBL userBL;
        private readonly ISecurityHelper securityHelper;

        public UserController(ISecurityHelper securityHelper, IUsersBL userBL)
        {
            this.securityHelper = securityHelper;
            this.userBL = userBL;
        }

        public UserController()
        {
            securityHelper = new SecurityHelper();
            userBL = new UsersBL();
        }

        [Authorize]
        public ActionResult Index(int? page)
        {
            try
            {
                if (!securityHelper.CheckIfUserIsAdmin(User.Identity.Name))
                {
                    return RedirectToAction("Fobridden", "Error");
                }
                var pageNumber = (page ?? 1);
                var res = userBL.GetAllUsers();
                if (res.BlAnswer == Answer.Success)
                {
                    return View(res.ObjectAnswer.OrderBy(i => i.USERID)
                        .ToPagedList(pageNumber, ApplicationSettings.pageSize));
                }
                return RedirectToAction("Error", "Error");
            }
            catch (Exception ex)
            {
                TempData["Error"] = "Blad: " + ex.Message;
                return RedirectToAction("Error", "Error");
            }
        }

        [Authorize]
        public ActionResult Create()
        {
            return User.Identity.Name != null && !securityHelper.CheckIfUserIsAdmin(User.Identity.Name)
                ? (ActionResult)RedirectToAction("Fobridden", "Error")
                : View();
        }

        [HttpGet]
        [Authorize]
        public ActionResult Edit(int id)
        {
            try
            {
                if (User.Identity.Name != null && !securityHelper.CheckIfUserIsAdmin(User.Identity.Name))
                {
                    return RedirectToAction("Fobridden", "Error");
                }
                var blAnswer = userBL.GetUserById(id);
                if (blAnswer.BlAnswer == Answer.Error)
                {
                    TempData["Error"] = blAnswer.Exception;
                    return RedirectToAction("Error", "Error");
                }
                if (blAnswer.SingleObjectAnswer == null)
                {
                    TempData["Error"] = "W systemie brak inwentaryzacji o podanym Id.";
                    return RedirectToAction("Error", "Error");
                }
                return View("ExecuteEdit", blAnswer.SingleObjectAnswer);
            }
            catch (Exception ex)
            {
                TempData["Error"] = ex.Message;
                return RedirectToAction("Error", "Error");
            }
        }

        [Authorize]
        [HttpPost]
        public ActionResult ExecuteEdit(USERS user)
        {
            try
            {
                if (User.Identity.Name != null && !securityHelper.CheckIfUserIsAdmin(User.Identity.Name))
                {
                    return RedirectToAction("Fobridden", "Error");
                }
                if (userBL.Update(user).BlAnswer == Answer.Success)
                {
                    return RedirectToAction("Index");
                }
                return RedirectToAction("Error", "Error");
            }
            catch (Exception ex)
            {
                TempData["Error"] = "Blad: " + ex.Message;
                return RedirectToAction("Error", "Error");
            }
        }

        [HttpPost]
        [Authorize]
        [ValidateAntiForgeryToken]
        public ActionResult Create(USERS user)
        {
            try
            {
                if (User.Identity.Name != null && !securityHelper.CheckIfUserIsAdmin(User.Identity.Name))
                {
                    return RedirectToAction("Fobridden", "Error");
                }
                user.ISACTIVE = true;
                userBL.Insert(user);
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                TempData["Error"] = "Blad: " + ex.Message;
                return RedirectToAction("Error", "Error");
            }
        }
    }
}